package com.jdcjk.project.main.view;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.baidu.mapapi.map.BaiduMap;
import com.jdcjk.Constant;
import com.jdcjk.GlobalParams;
import com.jdcjk.MainApp;
import com.jdcjk.R;
import com.jdcjk.UrlAddress;
import com.jdcjk.base.presenter.BasePresenter;
import com.jdcjk.base.view.BaseActivity;
import com.jdcjk.base.view.FragmentViewClickListener;
import com.jdcjk.chat.OpenFireConnectionManager;
import com.jdcjk.chat.enity.MessageBean;
import com.jdcjk.chat.view.AccountOfflineDialog;
import com.jdcjk.project.account.model.entity.Result;
import com.jdcjk.project.account.view.AccountActivity;
import com.jdcjk.project.login.model.entity.AppInfoResult;
import com.jdcjk.project.login.view.LoginActivity;
import com.jdcjk.project.main.presenter.MainPresenter;
import com.jdcjk.project.main.view.baidu.BDCarMonitorFragment;
import com.jdcjk.project.main.view.gaode.GDCarMonitorFragment;
import com.jdcjk.project.setting.PositionReportService;
import com.jdcjk.project.setting.view.AboutUsActivity;
import com.jdcjk.project.setting.view.OtherSettingActivity;
import com.jdcjk.project.setting.view.PositionReportSettingActivity;
import com.jdcjk.project.setting.view.UserInfoActivity;
import com.jdcjk.util.AppInfoUtil;
import com.jdcjk.util.CommonMethod;
import com.jdcjk.util.LocationUtil;
import com.jdcjk.util.PreferencesUtils;
import com.jdcjk.util.dtr.zxing.activity.CaptureActivity;
import com.jdcjk.view.MyRadioButton;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import okhttp3.Call;
import okhttp3.Request;

import static com.jdcjk.chat.OpenFireChatMessageListener.terminalClose;
import static com.jdcjk.chat.OpenFireChatMessageListener.updataWarningInfoNumber;
import static com.jdcjk.chat.OpenFireConnectionListener.restartLogin;

/**
 * Created by dong on 2016/12/9.
 * 主界面
 */
public class MainActivity extends BaseActivity<MainPresenter> implements FragmentViewClickListener, View.OnClickListener {
    private final String TAG = MainActivity.class.getSimpleName();

    private RadioGroup radioGroup;
    private MyRadioButton carList_radioButton, carMonitor_radioButton, carWarming_radioButton, carMore_radioButton;

    //报警列表布局
    private FrameLayout bottomTabBdRbWarming_fl;
    //所有未查看设备报警信息数量textview
    private TextView warningInfoNumber_textview;

    private CarListFragment carListFragment;
    private GDCarMonitorFragment GDCarMonitorFragment;
    private BDCarMonitorFragment BDCarMonitorFragment;
    private WarmingFragment warmingFragment;
    private CarMoreFragment carMoreFragment;
    //添加设备dialog
    private BindCarDialog bindCarDialog;
    private MainPresenter mainPresenter;
    //显示的fragment类型
    private String fragmentType;

    //重新登利dialog
    private AccountOfflineDialog accountOfflineDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public MainPresenter bindPresenter() {
        mainPresenter = new MainPresenter(getBaseContext());
        return mainPresenter;
    }

    @Override
    public void initContentView(Bundle savedInstanceState) {
        //检查app版本
        checkAppVersion();
        radioGroup = (RadioGroup) findViewById(R.id.bottom_tab_bar_rg);
        carList_radioButton = (MyRadioButton) findViewById(R.id.bottom_tab_bd_rb_list);
        carList_radioButton.setOnClickListener(this);
        carMonitor_radioButton = (MyRadioButton) findViewById(R.id.bottom_tab_bd_rb_monitor);
        carMonitor_radioButton.setOnClickListener(this);
        carWarming_radioButton = (MyRadioButton) findViewById(R.id.bottom_tab_bd_rb_warming);
//        carWarming_radioButton.setOnClickListener(this);
        bottomTabBdRbWarming_fl = (FrameLayout) findViewById(R.id.bottom_tab_bd_rb_warming_fl);
        bottomTabBdRbWarming_fl.setOnClickListener(this);
        carMore_radioButton = (MyRadioButton) findViewById(R.id.bottom_tab_bd_rb_more);
        carMore_radioButton.setOnClickListener(this);
        warningInfoNumber_textview = (TextView) findViewById(R.id.warning_info_number_tv);

        carListFragment = new CarListFragment();
        carListFragment.setFragmentViewClickListener(this);
        warmingFragment = new WarmingFragment();
        warmingFragment.setFragmentViewClickListener(this);
        carMoreFragment = new CarMoreFragment();
        carMoreFragment.setFragmentViewClickListener(this);
        addFragmentContent(carListFragment, R.id.main_fragment_content, 1);
        fragmentType = "carListFragment";
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if (checkedId == carList_radioButton.getId()) {
                    switchDiffFragmentContent(carListFragment, R.id.main_fragment_content, 1, false, false);
                    fragmentType = "carListFragment";
                } else if (checkedId == carMonitor_radioButton.getId()) {
                    //获得所设置的地图类型
                    String mapType = PreferencesUtils.getString(MainActivity.this, PreferencesUtils.MAP_TYPE);
                    //如果是百度地图，则加载相应的Fragment
                    if (mapType.equals("BaiDu")) {
                        //百度地图多车监控Fragment
                        if (BDCarMonitorFragment == null)
                            BDCarMonitorFragment = new BDCarMonitorFragment();

                        BDCarMonitorFragment.setFragmentViewClickListener(MainActivity.this);
                        switchDiffFragmentContent(BDCarMonitorFragment, R.id.main_fragment_content, 2, false, false);
                        fragmentType = "BDCarMonitorFragment";
                        BDCarMonitorFragment.setCarList(carListFragment.getCarInfoList());
                    } else {
                        //高德地图多车监控Fragment
                        if (GDCarMonitorFragment == null)
                            GDCarMonitorFragment = new GDCarMonitorFragment();

                        GDCarMonitorFragment.setFragmentViewClickListener(MainActivity.this);
                        switchDiffFragmentContent(GDCarMonitorFragment, R.id.main_fragment_content, 2, false, false);
                        fragmentType = "GDCarMonitorFragment";
                        GDCarMonitorFragment.setCarList(carListFragment.getCarInfoList());
                    }
                } else if (checkedId == carWarming_radioButton.getId()) {
                    //由于增加了未读信息数量红点，所以不能从这里点击切换界面
                } else if (checkedId == carMore_radioButton.getId()) {
                    switchDiffFragmentContent(carMoreFragment, R.id.main_fragment_content, 4, false, false);
                    fragmentType = "carMoreFragment";
                }
            }
        });
        //及时通讯初始化
        MainApp.getInstance().initXMPPTCPConnection();
        // 初始化EventBus
        EventBus.getDefault().register(this);
        //初始化Gps
        initGps();
        //开启位置上报Service
        startService(new Intent(this, PositionReportService.class));
        //获取未查看报警信息数量
        mainPresenter.getUnreadMsgNum(UrlAddress.GET_UNREAD_MSG_NUM, String.valueOf(PreferencesUtils.getInt(this, Constant.UserInfo.id)), new BasePresenter.OnUIThreadListener<String>() {
            @Override
            public void onBefore(Request request, int id) {

            }

            @Override
            public void onAfter(int id) {

            }

            @Override
            public void onError(Call call, Exception e, int i) {

            }

            @Override
            public void onResponse(String result) {
                int warningNumber = Integer.valueOf(result);
                //存储所有设备未查看报警信息数量
                PreferencesUtils.setInt(MainApp.getInstance(), PreferencesUtils.WARNING_INFO_NUMBER, warningNumber);
                String warningNumberInfo = warningNumber > 99 ? "99+" : String.valueOf(warningNumber);
                //判断报警信息数量，为0的话不显示数量信息红点
                warningInfoNumber_textview.setVisibility(warningNumber == 0 ? View.INVISIBLE : View.VISIBLE);
                //存储报警信息值
                warningInfoNumber_textview.setText(warningNumberInfo);
            }
        });
    }


    /**
     * 检查app版本是否为最新
     */
    private void checkAppVersion() {
        String appId = "80ad379df0178f0c18aa1c85bf78cae1";
        mainPresenter.getAppInfo(UrlAddress.GET_APP_GROUP_INFO, appId, "b5e4d90408d37a56eb2b8f3a2dd44050", new BasePresenter.OnUIThreadListener<AppInfoResult>() {
            @Override
            public void onBefore(Request request, int id) {

            }

            @Override
            public void onAfter(int id) {

            }

            @Override
            public void onError(Call call, Exception e, int i) {

            }

            @Override
            public void onResponse(AppInfoResult result) {
                if (result.getCode().equals("0")) {
                    List<AppInfoResult.DataBean> dataBeanList = result.getData();
                    final AppInfoResult.DataBean newestDataBean = dataBeanList.get(dataBeanList.size() - 1);
                    //获得网上app最新版本号
                    String newestVersion = newestDataBean.getAppVersion();
                    //获得当前app版本号
                    String localVersion = AppInfoUtil.getAppVersionName(MainActivity.this);
                    //判断版本
                    if (newestVersion.compareTo(localVersion) > 0) {
                        final UpdataAppDialog updataAppDialog = new UpdataAppDialog(MainActivity.this, newestDataBean.getAppQRCodeURL());
                        //弹出添加设备dialog
                        updataAppDialog.show(getFragmentManager(), "");
                    }
                }
            }
        });
    }

    private void initGps() {
        final OpenGpsDialog openGpsDialog = new OpenGpsDialog(this);
        openGpsDialog.setClickListenerInterface(new OpenGpsDialog.ClickListenerInterface() {
            @Override
            public void gpsCancle() {
                openGpsDialog.cancel();
            }

            @Override
            public void gpsOpen() {
                openGpsDialog.cancel();
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
            }
        });
        refreshGPSState();
        if (GlobalParams.gpsLongitude != 0 && GlobalParams.gpsLatitude != 0) {
        } else {
            //检测打开GPS
            if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                openGpsDialog.show();
            } else {
                Toast.makeText(this, "当前位置无GPS信号！", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private LocationManager locationManager;
    private LocationUtil locationUtil;

    public void refreshGPSState() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationUtil = LocationUtil.getInstance(this, LocationUtil.GET_LOCATION_ALWAYS);
        locationUtil.setOnLocationReceiveListener(new LocationUtil.OnLocationReceiveListenerAdapter() {
            @Override
            public void onLocationReceive(LocationUtil.MyLocation mBaseLocation) {
                GlobalParams.gpsLongitude = mBaseLocation.getLongitude();
                GlobalParams.gpsLatitude = mBaseLocation.getLatitude();
                GlobalParams.gpsAltitude = mBaseLocation.getAltitude();
                GlobalParams.gpsSpeed = (float) CommonMethod.myRound((double) mBaseLocation.getSpeed(), 2);
                GlobalParams.gpsDirection = mBaseLocation.getDirection();
            }
        });
        locationUtil.startMonitor();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bottom_tab_bd_rb_list:
                selectRadioButton(1);
                break;
            case R.id.bottom_tab_bd_rb_monitor:
                selectRadioButton(2);
                break;
            case R.id.bottom_tab_bd_rb_warming_fl:
                selectRadioButton(3);
                switchDiffFragmentContent(warmingFragment, R.id.main_fragment_content, 3, false, false);
                fragmentType = "warmingFragment";
                //清空未查看报警信息数量
                PreferencesUtils.setInt(MainActivity.this, PreferencesUtils.WARNING_INFO_NUMBER, 0);
                warmingFragment.setTerminalIdList(carListFragment.getTermianlIdList());
                warningInfoNumber_textview.setVisibility(View.INVISIBLE);
                radioGroup.check(bottomTabBdRbWarming_fl.getId());
                break;
            case R.id.bottom_tab_bd_rb_more:
                selectRadioButton(4);
                break;
            case R.id.dialog_sim_scan_btn:
                //添加设备dialog扫描二维码按钮监听
                //扫描二维码,启动二维码扫描界面
                startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 1);
                break;
            case R.id.dialog_add_car_determine_btn:
                //添加设备dialog确定按钮监听
                String account = PreferencesUtils.getString(this, Constant.UserInfo.account);
                String simNumber = bindCarDialog.getSimNumber();
                if (simNumber == null) {
                    Toast.makeText(this, R.string.terminal_sn_number_null, Toast.LENGTH_SHORT).show();
                    return;
                }
                mainPresenter.bindTerminal(UrlAddress.BIND_TERMINAL_FOR_USER, account, simNumber, new BasePresenter.OnUIThreadListener<Result>() {
                    @Override
                    public void onBefore(Request request, int id) {
                        bindCarDialog.setDetermineBtnClicked(false);
                    }

                    @Override
                    public void onAfter(int id) {

                    }

                    @Override
                    public void onError(Call call, Exception e, int i) {
                        Toast.makeText(MainActivity.this, R.string.system_exception, Toast.LENGTH_SHORT).show();
                        bindCarDialog.setDetermineBtnClicked(true);
                    }

                    @Override
                    public void onResponse(Result result) {
                        switch (result.getStatus()) {
                            case "OK":
                                Toast.makeText(MainActivity.this, result.getMessage(), Toast.LENGTH_SHORT).show();
                                //刷新界面
                                carListFragment.refreshView();
                                bindCarDialog.dismiss();
                                break;
                            case "NOT_EXIST":
                                Toast.makeText(MainActivity.this, result.getMessage(), Toast.LENGTH_SHORT).show();
                                break;
                            case "SYS_ERROR":
                                Toast.makeText(MainActivity.this, R.string.system_exception, Toast.LENGTH_SHORT).show();
                                break;
                        }
                        bindCarDialog.setDetermineBtnClicked(true);
                    }
                });
                break;
        }
    }

    /**
     * 根据buttonTag设置RadioButton状态，从底部栏左到右一次为1,2,3,4
     *
     * @param buttonTag
     */
    public void selectRadioButton(int buttonTag) {
        carList_radioButton.setChecked(buttonTag == 1);
        carMonitor_radioButton.setChecked(buttonTag == 2);
        carWarming_radioButton.setChecked(buttonTag == 3);
        carMore_radioButton.setChecked(buttonTag == 4);
    }

    @Override
    public void onFragmentViewClickListener(View v) {
        switch (v.getId()) {
            case R.id.fragment_car_monitor_map_type:
                switch (fragmentType) {
                    //高德地图多车监控界面
                    case "GDCarMonitorFragment":
                        if (GDCarMonitorFragment.getAMap().getMapType() == AMap.MAP_TYPE_NORMAL) {
                            GDCarMonitorFragment.getAMap().setMapType(AMap.MAP_TYPE_SATELLITE);
                            GDCarMonitorFragment.getAMap().showMapText(false);
                            GDCarMonitorFragment.getAMap().showBuildings(false);
                            GDCarMonitorFragment.getAMap().showIndoorMap(false);
                            GDCarMonitorFragment.getMapTypeImageView().setBackgroundResource(R.drawable.fragment_car_monitor_change_to_normal);
                        } else {
                            GDCarMonitorFragment.getAMap().setMapType(AMap.MAP_TYPE_NORMAL);
                            GDCarMonitorFragment.getMapTypeImageView().setBackgroundResource(R.drawable.fragment_car_monitor_change_to_satellite);
                        }
                        break;
                    //百度地图多车监控界面
                    case "BDCarMonitorFragment":
                        if (BDCarMonitorFragment.getBDMap().getMapType() == BaiduMap.MAP_TYPE_NORMAL) {
                            BDCarMonitorFragment.getBDMap().setMapType(BaiduMap.MAP_TYPE_SATELLITE);
                            BDCarMonitorFragment.getMapTypeImageView().setBackgroundResource(R.drawable.fragment_car_monitor_change_to_normal);
                        } else {
                            BDCarMonitorFragment.getBDMap().setMapType(BaiduMap.MAP_TYPE_NORMAL);
                            BDCarMonitorFragment.getMapTypeImageView().setBackgroundResource(R.drawable.fragment_car_monitor_change_to_satellite);
                        }
                        break;
                }
                break;
            case R.id.fragment_car_monitor_previous:
                switch (fragmentType) {
                    //高德地图多车监控界面
                    case "GDCarMonitorFragment":
                        GDCarMonitorFragment.setCurrentIndex(GDCarMonitorFragment.getCurrentIndex() - 1);
                        GDCarMonitorFragment.animateToLocation(GDCarMonitorFragment.getCarLatLngArray().get(GDCarMonitorFragment.getCurrentIndex() % GDCarMonitorFragment.getCarLatLngArray().size()));
                        //设置显示窗口等级为1
                        GDCarMonitorFragment.setCarWindowsLevel(1);
                        GDCarMonitorFragment.getCarMarkerArray().get(GDCarMonitorFragment.getCurrentIndex() % GDCarMonitorFragment.getCarLatLngArray().size()).showInfoWindow();
                        break;
                    //百度地图多车监控界面
                    case "BDCarMonitorFragment":
                        BDCarMonitorFragment.setCurrentIndex(BDCarMonitorFragment.getCurrentIndex() - 1);
                        BDCarMonitorFragment.animateToLocation(BDCarMonitorFragment.getCarLatLngArray().get(BDCarMonitorFragment.getCurrentIndex() % BDCarMonitorFragment.getCarLatLngArray().size()));
                        //设置显示窗口等级为1
                        BDCarMonitorFragment.setCarWindowsLevel(1);
                        BDCarMonitorFragment.showInfoWindow(BDCarMonitorFragment.getCarMarkerArray().get(BDCarMonitorFragment.getCurrentIndex() % BDCarMonitorFragment.getCarLatLngArray().size()));
                        break;
                }

                break;
            case R.id.fragment_car_monitor_next:
                switch (fragmentType) {
                    //高德地图多车监控界面
                    case "GDCarMonitorFragment":
                        GDCarMonitorFragment.setCurrentIndex(GDCarMonitorFragment.getCurrentIndex() + 1);
                        GDCarMonitorFragment.animateToLocation(GDCarMonitorFragment.getCarLatLngArray().get(GDCarMonitorFragment.getCurrentIndex() % GDCarMonitorFragment.getCarLatLngArray().size()));
                        //设置显示窗口等级为1
                        GDCarMonitorFragment.setCarWindowsLevel(1);
                        GDCarMonitorFragment.getCarMarkerArray().get(GDCarMonitorFragment.getCurrentIndex() % GDCarMonitorFragment.getCarLatLngArray().size()).showInfoWindow();
                        break;
                    //百度地图多车监控界面
                    case "BDCarMonitorFragment":
                        BDCarMonitorFragment.setCurrentIndex(BDCarMonitorFragment.getCurrentIndex() + 1);
                        BDCarMonitorFragment.animateToLocation(BDCarMonitorFragment.getCarLatLngArray().get(BDCarMonitorFragment.getCurrentIndex() % BDCarMonitorFragment.getCarLatLngArray().size()));
                        //设置显示窗口等级为1
                        BDCarMonitorFragment.setCarWindowsLevel(1);
                        BDCarMonitorFragment.showInfoWindow(BDCarMonitorFragment.getCarMarkerArray().get(BDCarMonitorFragment.getCurrentIndex() % BDCarMonitorFragment.getCarLatLngArray().size()));
                        break;
                }

                break;
            case R.id.fragment_car_more_exit_btn:
                //删除对应的登录用户信息
                PreferencesUtils.deleteUserInfo(this);
                //更多界面 退出账号button监听
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                //释放openfire连接
                OpenFireConnectionManager.getInstance().release();
                finish();
                break;
            case R.id.fragment_car_add_btn:
                //弹出添加设备dialog
                bindCarDialog = new BindCarDialog(this, this);
                bindCarDialog.show(getFragmentManager(), "");
                break;
            case R.id.car_more_other_setting_rl:
                //更多界面报警设置布局点击监听，跳转到设置activity
                intent = new Intent(this, OtherSettingActivity.class);
                startActivity(intent);
                break;
            case R.id.car_more_position_report_setting_rl:
                //更多界面位置上报设置布局点击监听，跳转到设置activity
                intent = new Intent(this, PositionReportSettingActivity.class);
                startActivity(intent);
                break;
            case R.id.about_us_rl:
                //更多界面关于我们布局点击监听，跳转到设置activity
                intent = new Intent(this, AboutUsActivity.class);
                startActivity(intent);
                break;
            case R.id.updata_pwd_rl:
                //更多界面修改密码布局点击监听，跳转到忘记密码activity
                intent = new Intent(this, AccountActivity.class);
                //设置需要弹出的界面（registered为注册界面，retrieve_pwd为找回密码界面）
                intent.putExtra(Constant.FragmentInfo.fragment, "retrieve_pwd");
                startActivity(intent);
                break;
            case R.id.my_account_rl:
                //更多界面我的账号布局点击监听，跳转到我的信息activity
                intent = new Intent(this, UserInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.car_info_refresh_btn:
                //多车监控界面刷新按钮监听
                //刷新车辆信息
                switch (fragmentType) {
                    //高德地图多车监控界面
                    case "GDCarMonitorFragment":
                        GDCarMonitorFragment.refreshCarInfo();
                        break;
                    //百度地图多车监控界面
                    case "BDCarMonitorFragment":
                        BDCarMonitorFragment.refreshCarInfo();
                        break;
                }

                break;
            case R.id.refresh_btn:
                //报警列表界面刷新按钮监听
                //刷新报警信息
                warmingFragment.initData();
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 1:
                //获得设备信息返回设局
                Bundle bundle = data.getExtras();
                String displayid = bundle.getString(Constant.TerminalInfo.displayid);//设备名称
                String carId = bundle.getString(Constant.TerminalInfo.carId);//设备车牌号
                String cardNum = bundle.getString(Constant.TerminalInfo.cardNum);//设备手机卡号
                //更新当前设备信息
                GDCarMonitorFragment.updataCarListInfo(displayid, carId, cardNum);
                break;
            case 2:
                String result = data.getExtras().getString("result");//得到二维码扫描后后返回的数据
                bindCarDialog.setSimNumber(result);
                break;
        }
    }

    /**
     * 接收收到的设备状态信息
     *
     * @param terminalStatusBean 位置信息
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getLatlngByEventBus(MessageBean.TerminalStatusBean terminalStatusBean) {
        //判断当前所在的fragment
        switch (fragmentType) {
            //车辆列表界面
            case "carListFragment":
                //如果是当前车辆的位置信息，则更新车辆数据
                carListFragment.updataCarInfo(terminalStatusBean);
                break;
            //高德地图多车监控界面
            case "GDCarMonitorFragment":
                GDCarMonitorFragment.updataCarInfo(terminalStatusBean);
                break;
            //百度地图多车监控界面
            case "BDCarMonitorFragment":
                BDCarMonitorFragment.updataCarInfo(terminalStatusBean);
                break;
        }

    }

    /**
     * 接收设备关闭信息或者更新报警信息数量
     *
     * @param message 位置信息
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getLatlngByEvenBus(String message) {
        if (message.equals(terminalClose)) {
            //如果是关闭设备
            //判断当前所在的fragment
            switch (fragmentType) {
                //车辆列表界面
                case "carListFragment":
                    //如果是当前车辆的位置信息，则更新车辆数据
                    carListFragment.closeAllTerminal();
                    break;
                //高德地图多车监控界面
                case "GDCarMonitorFragment":
                    GDCarMonitorFragment.closeAllTerminal();
                    break;
                //百度地图多车监控界面
                case "BDCarMonitorFragment":
                    BDCarMonitorFragment.closeAllTerminal();
                    break;
            }
        }
        if (message.equals(restartLogin)) {
            if (accountOfflineDialog == null || accountOfflineDialog.isHidden()) {
                //如果是弹出重新登录dialog
                accountOfflineDialog = new AccountOfflineDialog();
                accountOfflineDialog.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //重新登录按钮监听
                        //及时通讯初始化
                        MainApp.getInstance().initXMPPTCPConnection();
                        accountOfflineDialog.dismiss();
                        accountOfflineDialog = null;
                    }
                });
                accountOfflineDialog.show(getFragmentManager(), "");
            }
        }
        if (message.contains(updataWarningInfoNumber)) {
            //获得积累的未阅读的所有报警信息数量
            int warningNumber = PreferencesUtils.getInt(MainApp.getInstance(), PreferencesUtils.WARNING_INFO_NUMBER);
            String carWarningNumber = warningNumber > 99 ? "99+" : String.valueOf(warningNumber);
            warningInfoNumber_textview.setVisibility(View.VISIBLE);
            warningInfoNumber_textview.setText(String.valueOf(carWarningNumber));
        }
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(false);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
